// **********************************************
// * APM_CreateMine [1.0]                       *
// * CreateMine module for AP Mines             *
// * by Prodavec                                *
// **********************************************

// DEFINITIONS
// ----------------------------------------------

// PRE-INIT
// ----------------------------------------------

// VARIABLES
// ----------------------------------------------

// FUNCTIONS
// ----------------------------------------------

// MAIN
// ----------------------------------------------

// player side and position
_side = side player;
_position = getPosATL player;

// What kind of mine(s) do we have?
_magazines = magazines player;
_eastMineIndex = _magazines find "HandGrenade_East";
_westMineIndex = _magazines find "HandGrenade_West";

// What kind of mine will we use?
_selectedMine = nil;
if (((_side == east) && (_eastMineIndex != -1)) || ((_side == west) && (_westMineIndex == -1))) then
{
	_selectedMine = "HandGrenade_East";
}
else
{
	_selectedMine = "HandGrenade_West";
};

if (isNil "_selectedMine") exitWith {}; // Just for case

// Drop grenade on the ground, it will became Weapon Holder
player action ["dropMagazine", _position, _selectedMine];
sleep 1.9;

// Delete Weapon Holder
deleteVehicle ((nearestObjects [_position, ["WeaponHolder"], 2]) select 0);

// Create Explosion Device and wait a bit
_explosiveDevice = createVehicle ["Explosive", _position, [], 0, "CAN_COLLIDE"];
sleep 5;

// Create explosion trigger for device
_explosionTrigger = createTrigger ["EmptyDetector", _position];
_explosionTrigger setTriggerArea [10, 10, 0, false];
_explosionTrigger setTriggerActivation ["ANY", "PRESENT", false];

// Arrange trigger array by deleting old trigger records
call fnc_APM_ArrangeTriggerArray;
// Register new trigger in array
APM_ExplosionTriggerArray set [count APM_ExplosionTriggerArray, _explosionTrigger];
// Setting up all needed fields of the trigger
call compile format ["_explosionTrigger setTriggerStatements ['[%1, thislist] call fnc_APM_CheckTriggerConditions', '[%1, trigger] spawn fnc_APM_Explode', ''];", ((count APM_ExplosionTriggerArray) - 1)];